package com.stedu.controller;

import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.service.AdminService;
import com.stedu.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
 * 获取已经登陆用户信息
 * */
@CrossOrigin
@RestController
@RequestMapping("/admin/info")
public class InfoController {
    @Autowired
    private AdminService adminService;

    //后端接收并解析前端传来的jwt
    @GetMapping("/loginInfo")
    public RespBean loginInfo(HttpServletRequest request) {
        //获取jwt
        String token = request.getHeader("token");
        //解析jwt
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        //通过id查询用户信息
        //从上层往下层定义方法，规定返回值类型
        Admin admin = adminService.selectById(id);

        return RespBean.ok("", admin);
    }

    @GetMapping("/top5Dept")
    public RespBean top5Dept() {
        List<Map<String, Object>> maps = adminService.selectTop5Dept();
        return RespBean.ok("", maps);
    }

    @GetMapping("/classNumber")
    public RespBean classNumber() {
        List<Map<String, Object>> maps = adminService.selectClassNumber();
        return RespBean.ok("", maps);
    }
}
